package com.qf.shop.controller;


import com.qf.shop.entity.SysUser;

import com.qf.shop.mapper.SysUserMapper;
import com.qf.shop.model.Results;

import com.qf.shop.service.ISysUserService;

import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ Author     ：lzq.
 * @ Date       ：Created in 19:54 2020/10/23
 */
@RestController
@CrossOrigin(origins = "*")  //跨域配置
@RequestMapping("/sysUser")
public class SysUserController {
    private SysUserMapper sysUserMapper;
    private ISysUserService sysUserServiceImpl;

    public SysUserController(SysUserMapper sysUserMapper, ISysUserService sysUserServiceImpl) {
        this.sysUserMapper = sysUserMapper;
        this.sysUserServiceImpl = sysUserServiceImpl;
    }


    @RequestMapping("/login")
    public Results sysLogin(HttpSession session, HttpServletResponse resp, String username, String password) {
//验证用户名和密码是否正确，正确返回用户ID
        Results results = null;
        try {
            Integer userId = sysUserServiceImpl.chewSysLogin(username, password);

            if (userId != null) {
                session.setAttribute("user", username);
                System.out.println("建立session");
                //将userID放到cookie中，时效是一天
                Cookie cookie = new Cookie("userId", username);
                System.out.println("cookieName:" + cookie.getName());
                cookie.setMaxAge(1 * 24 * 60 * 60);
                cookie.setPath("/");
                System.out.println("准备建立cookie");
                resp.addCookie(cookie);
                System.out.println("登录成功建立cookie");

                results = new Results();
                results.setMsg("success");
                results.setCode(1);
                System.out.println(results + "后台登录信息");
            }
        } catch (Exception e) {
            System.out.println("后端登录异常");
            results = new Results();
            results.setMsg("error");
            results.setCode(-1);
            e.printStackTrace();
        }

        return results;
    }

    @GetMapping("/loginOut")
    public Results logout(HttpSession session, HttpServletRequest req, HttpServletResponse resp) {
        //删除session
        System.out.println("退出登录，清除session1");
        session.removeAttribute("user");
        System.out.println("退出登录，清除session2");
        //删除cookie
        Cookie[] cookies = req.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                System.out.println("cookie:"+cookie.getName());
                if ("userId".equals(cookie.getName())) {
                    Cookie cookie1 = new Cookie("userId", null);
                    cookie1.setMaxAge(0);
                    cookie1.setPath("/");
                    resp.addCookie(cookie1);
                    System.out.println("退出登录，清除cookie");
                    break;
                }
            }
        }
        System.out.println("---清理完毕");
        Results re = new Results();
        re.setMsg("注销成功");
        return re;
    }

    @GetMapping("getAll")
    public List<SysUser> getSysUsers(@Param("id") Integer id, @Param("sname") String sname, @Param("password") String password) {
        List<SysUser> sysUserList = sysUserServiceImpl.getAllSysData(id, sname, password);
        return sysUserList;
    }

    @GetMapping("getSysUer/{id}")
    public SysUser getSysUser(@PathVariable("id") Integer id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        return sysUser;
    }

    @DeleteMapping("deleteSysUer/{id}")
    public Integer deleteSysUer(@PathVariable("id") Integer id) {
        return sysUserMapper.deleteById(id);

    }

    @PostMapping("add")
    public Integer addSysUer(SysUser sysUser) {
        return sysUserMapper.insert(sysUser);
    }


    @GetMapping("/determineLogin")
    public Results determineLogin() {
        Results results=new Results();
return results;
    }
}
        //           Results results=new Results();
//
//        SysUser sysUser=iSysUserServiceImpl.chewSysLogin(username);
//        if (sysUser!=null && password.equals(sysUser.getPassword())){
//            results.setCode(1);
//            results.setMsg("success");
//        }
//         else {
//            results.setCode(-1);
//            results.setMsg("error");
//        }
//
//        return results;



    //    /**
//     * 跳转登录
//     */
//    @PostMapping("/login")
////    public String login(SysUser sysUser, Model model, HttpSession session) {
////        //获取用户名和密码
////        String username = sysUser.getSname();
////        String password = sysUser.getPassword();
////        //些处横板从数据库中获取对用户名和密码后进行判断
////        if (username != null && username.equals("admin") && password != null && password.equals("admin")) {
////            //将用户对象添加到Session中
////            session.setAttribute("USER_SESSION", sysUser);
////            //重定向到主页面的跳转方法
////            return "redirect:main";
////        }
////        model.addAttribute("msg", "用户名或者错误，请重新登录！");
////        return "login";
////    }
////
////         @RequestMapping("/main")
////         public String toMain() {
////          return "main";
////        }
////
////    @RequestMapping("/logout")
////    public String logout(HttpSession session) {
////        //清除session
////        session.invalidate();
////        //重定向到登录页面的跳转方法
////        return "redirect:login";
////    }




